package com.nike.amoeba.ui.frg

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.adapter.StockOrderListAdapter
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.databinding.BNotTitleRecyclerBinding
import com.nike.amoeba.ext.WAREHOUSE_ORDER_TYPE
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadListData
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.setNbOnItemClickListener
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.RfidModel
import com.nike.amoeba.viewmodel.StockManageModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import me.hgj.jetpackmvvm.ext.nav

/**
 * User: Nike
 *  2024/7/17 16:35
 *      仓库订单列表
 */
class StockManageFrg : BaseFragment<StockManageModel, BNotTitleRecyclerBinding>() {

    val adapter: StockOrderListAdapter by lazy { StockOrderListAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    val stockManageModel: StockManageModel by activityViewModels()

    var searchText: String? = null

    val rfidModel: RfidModel by activityViewModels()

    var stockType = 0

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.code.set(it.getString("code"))
            stockType = it.getInt("type")
        }
        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            lazyLoadData()
        }
        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }
        mViewModel.onWarehouseOrderList(stockType, mViewModel.code.get())
        mDatabind.swipeRefresh.setOnRefreshListener {
            mViewModel.onWarehouseOrderList(stockType, mViewModel.code.get())
        }
        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = mFilterList[position] as DataBean
                UIHelper.starStockManageListFrg(nav(), bean.BatchNo, stockType, mViewModel.code.get())
            }
        }

    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadListData(it, adapter, loadsir, mDatabind.recyclerView, mDatabind.swipeRefresh, it.pageSize)
            adapter!!.appendList(it.listData)
        })
        stockManageModel.searchText.observe(viewLifecycleOwner, {
            searchText = it
            adapter!!.filter.filter(searchText)
        })
        //扫码
        eventViewModel.zkingType.observeInFragment(this, Observer {
            if (it.Type == WAREHOUSE_ORDER_TYPE){
                toWarehouseListFrg(it.text)
            }
        })
        rfidModel.scanData.observe(viewLifecycleOwner, { scanData ->
            if (scanData != null && viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                toWarehouseListFrg(scanData)
            }
        })
    }

    private fun toWarehouseListFrg(scanData: String?) {
        val bean = adapter.mFilterList.firstOrNull { it.BatchNo == scanData }
        bean?.let { UIHelper.starStockManageListFrg(nav(), bean.BatchNo, stockType, mViewModel.code.get()) }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        //设置界面 加载中
//        loadsir.showLoading()
//        mViewModel.onWarehouseOrderList(stockType, mViewModel.code.get())
    }

}
